<template>
	<uv-popup :closeOnClickOverlay="false" :customStyle="{backgroundColor: 'transparent'}" ref="popupRef" mode="center"
		:showConfirmButton="false">
		<view class="popup">
			<view class="popup-body">
				<view class="popup-body-head">
					请输入验证码
				</view>
				<view class="popup-body-tips">
					<view class="">
						验证码已发送至+86 {{ phonedesensitize }}
					</view>
					<view @click="getCode" :style="{color: !codeDisable ? '#1277FF' : '#666'}"
						style="font-size: 26rpx; line-height: 44rpx;">
						{{ codeText }}
					</view>
				</view>
				<view class="popup-body-code">
					<yi-code inputType="number" @onComplete="codeComplete" ref="yiCode" :width="584" :maxlength="6" type="block"></yi-code>
				</view>
				<view class="popup-body-btn">
					<text @click="codeComplete">获取额度</text>
				</view>
				<image @click="close" class="popup-body-close" src="/static/images/close.png" mode=""></image>
				<view class="popup-body-style-box">
					<image @click="close" class="popup-body-style-img" src="/static/images/index/yzm.png" mode="">
					</image>
				</view>
			</view>
		</view>
	</uv-popup>
</template>

<script>
	export default {
		name: 'verificationCode',
		data() {
			return {
				codeText: "获取验证码",
				codeDisable: false,
				codeLoading: false,
				interval: null,
				phone: "",
			}
		},
		computed: {
			phonedesensitize() {
				if (!this.phone) return ''
				return `${this.phone.slice(0, 3)}****${this.phone.slice(this.phone.length - 3, this.phone.length)}`
			}
		},
		mounted() {
			// this.$refs.popupRef.open();
		},
		methods: {
			codeAwait(time) {
				clearInterval(this.interval);
				this.codeDisable = true;
				this.codeText = `${time}s`;
				this.interval = setInterval(() => {
					if (time <= 1) {
						this.codeDisable = false;
						this.codeText = '获取验证码';
						return clearInterval(this.interval);
					}
					this.codeText = `${--time}s`;
				}, 1000);
			},
			open(phone) {
				if (this.phone != phone) {
					this.codeDisable = false;
					this.codeText = '获取验证码';
					clearInterval(this.interval)
					this.phone = phone;
				}
				this.code = '';
				if (!this.codeDisable) {
					this.getCode()
				}
				this.$refs.popupRef.open();
				this.$refs.yiCode.setFocus()
			},
			getCode() {
				this.$emit('getCode');
				this.codeAwait(60);
			},
			close() {
				this.$refs.popupRef.close()
			},
			codeComplete(str) {
				console.log((str));
				if (str.length != 6) {
					return uni.showToast({
						title: '请输入正确的验证码',
						icon: 'none'
					})
				}
				this.$emit('onComplete', str);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup {
		overflow: hidden;
	}

	.popup-body {
		box-sizing: border-box;
		width: 654rpx;
		// height: 496rpx;
		margin-top: 88rpx;
		padding: 80rpx 32rpx 32rpx 32rpx;
		background: linear-gradient(180deg, #DCEFFF 0%, #FFFFFF 100%);
		border-radius: 16px;
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;

		&-style-img {
			width: 112rpx;
			height: 128rpx;
		}

		&-style-box {
			width: 160rpx;
			height: 160rpx;
			background-color: #fff;
			border-radius: 50%;
			position: absolute;
			display: flex;
			justify-content: center;
			align-items: center;
			top: -88rpx;
		}

		&-close {
			position: absolute;
			z-index: 10001;
			width: 32rpx;
			height: 32rpx;
			top: 32rpx;
			right: 32rpx;
			filter: grayscale(1) brightness(0.7)
		}

		&-code {
			width: 100%;
		}

		&-btn {
			width: 100%;
			margin-top: 40rpx;
			padding-top: 24rpx;
			border-top: 2rpx solid #D8D8D8;
			display: flex;
			justify-content: center;
			font-size: 32rpx;
			color: #1277FF;
			line-height: 44rpx;
		}

		&-tips {
			width: 100%;
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			color: #666666;
			line-height: 40rpx;
			margin-bottom: 32rpx;
		}

		&-head {
			font-size: 32rpx;
			color: #0A225C;
			font-weight: 600;
			line-height: 48rpx;
			margin-bottom: 12rpx;
		}
	}
</style>